"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CoreStorageAsync = exports.CoreStorage = void 0;
class CoreStorage {
    constructor(key = 'g') {
        this.key = key;
        this.apt = localStorage;
    }
    setKey(k) {
        this.key = k;
    }
    getKey(k) {
        return `@@${this.key}-${k}@@`;
    }
    setApt(apt) {
        this.apt = apt;
    }
    get(key) {
        try {
            const pre = this.apt.getItem(this.getKey(key)) || '';
            return JSON.parse(pre).value;
        }
        catch (err) {
            return null;
        }
    }
    set(key, v) {
        return this.apt.setItem(this.getKey(key), JSON.stringify({
            date: Date.now(),
            value: v,
        }));
    }
    remove(key) {
        this.apt.removeItem(this.getKey(key));
    }
    clear() {
        Object.keys(this.apt).forEach((key) => {
            if (key.match(`@@${this.key}-`)) {
                this.apt.removeItem(key);
            }
        });
    }
}
exports.CoreStorage = CoreStorage;
class CoreStorageAsync {
    constructor(key = 'g') {
        this.key = key;
        this.apt = localStorage;
    }
    setKey(k) {
        this.key = k;
    }
    getKey(k) {
        return `@@${this.key}-${k}@@`;
    }
    setApt(apt) {
        this.apt = apt;
    }
    get(key) {
        return Promise.resolve(this.apt.getItem(this.getKey(key))).then((r) => {
            try {
                if (typeof r === 'string')
                    return JSON.parse(r).value;
                return r.value;
            }
            catch (err) {
                return null;
            }
        });
    }
    set(key, v) {
        return Promise.resolve(this.apt.setItem(this.getKey(key), JSON.stringify({
            date: Date.now(),
            value: v,
        })));
    }
    remove(key) {
        return Promise.resolve(this.apt.removeItem(this.getKey(key)));
    }
    clear() {
        return Promise.resolve(this.apt.clear());
    }
}
exports.CoreStorageAsync = CoreStorageAsync;
